package com.ruoyi.biz.controller;

import com.ruoyi.biz.dto.LotteryLogDTO;
import com.ruoyi.biz.dto.LotteryPrizeDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryLogDO;
import com.ruoyi.biz.entity.LotteryPrizeDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.service.LotteryLogService;
import com.ruoyi.biz.service.LotteryPrizeService;
import com.ruoyi.biz.service.LotteryResultService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/biz/lotteryLog")
public class LotteryLogController extends BaseController {

    private String prefix = "lotteryLog";

    @Resource
    LotteryLogService lotteryLogService;
    @Resource
    LotteryPrizeService lotteryPrizeService;
    @Resource
    LotteryResultService lotteryResultService;

    @GetMapping("/{lotteryId}")
    public String build(@PathVariable("lotteryId") String lotteryId, ModelMap mmap) {
        mmap.put("lottery", lotteryId);
        LotteryPrizeDTO qLotteryPrizeDTO = new LotteryPrizeDTO();
        qLotteryPrizeDTO.setLotteryId(lotteryId);
        qLotteryPrizeDTO.setValiFlag("1");
        List<LotteryPrizeDO> lotteryPrizeDTOS = lotteryPrizeService.queryList(qLotteryPrizeDTO);
        mmap.put("lotteryPrizeDTOS", lotteryPrizeDTOS);
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LotteryLogDTO lotteryLogDTO) {
        startPage();
        List<LotteryLogDO> list = lotteryLogService.queryList(lotteryLogDTO);
        return getDataTable(list);
    }

    @Log(title = "抽奖记录管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        LotteryLogDTO qLotteryLogDTO = new LotteryLogDTO();
        qLotteryLogDTO.setLotteryLogId(ids);
        qLotteryLogDTO.setValiFlag("1");
        List<LotteryLogDO> lotteryLogDOS = lotteryLogService.queryList(qLotteryLogDTO);
        if (ValidateUtil.isEmpty(lotteryLogDOS)) {
            return error("系统错误，未查询到抽奖记录！");
        }
        if (!ValidateUtil.isEmpty(lotteryLogDOS.get(0).getLotteryResultId())) {
            LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
            qLotteryResultDTO.setLotteryId(lotteryLogDOS.get(0).getLotteryId());
            qLotteryResultDTO.setValiFlag("1");
            qLotteryResultDTO.setLotteryResultId(lotteryLogDOS.get(0).getLotteryResultId());
            List<LotteryResultDO> lotteryResultDOS = lotteryResultService.queryList(qLotteryResultDTO);
            if (ValidateUtil.isEmpty(lotteryResultDOS)) {
                return error("系统错误，未查询到抽奖结果信息！");
            }
            return toAjax(lotteryLogService.deleteLotteryResultAndLog(lotteryLogDOS.get(0).getLotteryLogId(), lotteryResultDOS.get(0)));
        } else {
            return toAjax(lotteryLogService.deleteLotteryLog(ids));
        }
    }
}
